// inventory.ts
Page({
  data: {
    inventoryData: [
      { type: '92', name: '92号汽油', stock: 5000, capacity: 10000, unit: '升' },
      { type: '95', name: '95号汽油', stock: 4500, capacity: 10000, unit: '升' },
      { type: '98', name: '98号汽油', stock: 3000, capacity: 8000, unit: '升' },
      { type: 'diesel', name: '0号柴油', stock: 6000, capacity: 12000, unit: '升' }
    ],
    editingIndex: -1,
    tempStock: '',
    tempCapacity: ''
  },

  onLoad() {
    this.loadInventory()
  },

  loadInventory() {
    const storedInventory = wx.getStorageSync('inventoryData')
    if (storedInventory && storedInventory.length > 0) {
      this.setData({
        inventoryData: storedInventory
      })
    }
  },

  startEdit(e: any) {
    const index = e.currentTarget.dataset.index
    const item = this.data.inventoryData[index]
    
    this.setData({
      editingIndex: index,
      tempStock: item.stock.toString(),
      tempCapacity: item.capacity.toString()
    })
  },

  onStockInput(e: any) {
    this.setData({
      tempStock: e.detail.value
    })
  },

  onCapacityInput(e: any) {
    this.setData({
      tempCapacity: e.detail.value
    })
  },

  saveInventory() {
    const { editingIndex, tempStock, tempCapacity, inventoryData } = this.data
    
    if (!tempStock || isNaN(parseInt(tempStock))) {
      wx.showToast({
        title: '请输入有效库存',
        icon: 'error'
      })
      return
    }

    if (!tempCapacity || isNaN(parseInt(tempCapacity))) {
      wx.showToast({
        title: '请输入有效容量',
        icon: 'error'
      })
      return
    }

    const stock = parseInt(tempStock)
    const capacity = parseInt(tempCapacity)

    if (stock > capacity) {
      wx.showToast({
        title: '库存不能超过容量',
        icon: 'error'
      })
      return
    }

    const updatedInventory = [...inventoryData]
    updatedInventory[editingIndex].stock = stock
    updatedInventory[editingIndex].capacity = capacity

    this.setData({
      inventoryData: updatedInventory,
      editingIndex: -1,
      tempStock: '',
      tempCapacity: ''
    })

    wx.setStorageSync('inventoryData', updatedInventory)
    
    wx.showToast({
      title: '库存更新成功',
      icon: 'success'
    })
  },

  cancelEdit() {
    this.setData({
      editingIndex: -1,
      tempStock: '',
      tempCapacity: ''
    })
  },

  addStock(e: any) {
    const index = e.currentTarget.dataset.index
    const amount = e.currentTarget.dataset.amount
    
    wx.showModal({
      title: '补充库存',
      content: `确定要补充${amount}升库存吗？`,
      success: (res) => {
        if (res.confirm) {
          const updatedInventory = [...this.data.inventoryData]
          const newStock = Math.min(
            updatedInventory[index].stock + parseInt(amount),
            updatedInventory[index].capacity
          )
          updatedInventory[index].stock = newStock

          this.setData({
            inventoryData: updatedInventory
          })

          wx.setStorageSync('inventoryData', updatedInventory)
          
          wx.showToast({
            title: '库存补充成功',
            icon: 'success'
          })
        }
      }
    })
  },

  getStockPercentage(stock: number, capacity: number) {
    return Math.round((stock / capacity) * 100)
  },

  getStockStatus(stock: number, capacity: number) {
    const percentage = this.getStockPercentage(stock, capacity)
    if (percentage < 20) return 'low'
    if (percentage < 50) return 'medium'
    return 'high'
  }
})